package com.zl.freight.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zl.freight.R;
import com.zl.freight.base.BaseActivity;
import com.zl.freight.mode.BaseUserEntity;
import com.zl.freight.utils.API;
import com.zl.freight.utils.SoapCallback;
import com.zl.freight.utils.SoapUtils;
import com.zl.freight.utils.SpUtils;
import com.zl.zlibrary.utils.GsonUtils;

import org.json.JSONArray;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhanglei
 * @date 18/05/25
 * 认证司机
 */
public class RealDriverActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.et_driver_name)
    EditText etDriverName;
    @BindView(R.id.et_driver_id_number)
    EditText etDriverIdNumber;
    private AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_driver);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        tvTitle.setText("司机认证");

        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        alertDialog = builder.setTitle("认证信息").setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).create();

        new android.support.v7.app.AlertDialog.Builder(mActivity)
                .setMessage("认证司机信息将会扣除" + SpUtils.getLoginAfter(mActivity).getRealDriver() + "元钱，望知晓。")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).show();
    }

    @OnClick({R.id.iv_back, R.id.bt_real})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_real:
                realDriver();
                break;
        }
    }

    /**
     * 认证司机
     */
    private void realDriver() {
        final String driverName = etDriverName.getText().toString().trim();
        final String driverIdNumber = etDriverIdNumber.getText().toString().trim();

        if (TextUtils.isEmpty(driverName)) {
            showToast("司机姓名不能为空");
            return;
        }

        if (TextUtils.isEmpty(driverIdNumber)) {
            showToast("司机身份证号不能为空");
            return;
        }

        Map<String, String> params = new HashMap<>();
        params.put("UserName", driverIdNumber);
        params.put("UserId", SpUtils.getUserData(mActivity).getId());
        showDialog();
        SoapUtils.Post(mActivity, API.QueryIdentity, params, new SoapCallback() {
            @Override
            public void onError(String error) {
                hideDialog();
                alertDialog.setMessage("货车多无法查询到此人的信息,此人可能在注册时没有填写真实姓名");
                alertDialog.show();
            }

            @Override
            public void onSuccess(String data) {
                hideDialog();
                try {
                    JSONArray array = new JSONArray(data);
                    alertDialog.setMessage("货车多无法查询到此人的信息,此人可能在注册时没有填写真实姓名");
                    for (int i = 0; i < array.length(); i++) {
                        BaseUserEntity baseUserEntity = GsonUtils.fromJson(array.optString(i), BaseUserEntity.class);
                        if (baseUserEntity.getIdCardNumber().equals(driverIdNumber) && baseUserEntity.getRealName().equals(driverName)) {
                            alertDialog.setMessage("此人是货车多的真实注册用户");
                            break;
                        }
                    }
                    alertDialog.show();
                } catch (Exception e) {

                }

            }
        });
    }
}
